package co.edu.aplicacion.persistencia.impl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import co.edu.aplicacion.Artista;
import co.edu.aplicacion.persistencia.def.IDAOGenerico;
import co.edu.aplicacion.persistencia.fwk.ArtistaFactory;
import co.edu.aplicacion.persistencia.fwk.ConexionBD;
import co.edu.aplicacion.persistencia.fwk.GeneradorLlave;

public class DAOArtista implements IDAOGenerico<Artista> {

	@Override
	public List<Artista> consultarTodos() throws Exception {
		List<Artista> artistas = new ArrayList<Artista>();
		PreparedStatement ps = null;
		try {
			ConexionBD conexionBD = ConexionBD.getInstance();
			Connection con = conexionBD.getCon();
			ps = con.prepareStatement("select * from artista_art order by cart_nombre");
			ResultSet rs = ps.executeQuery();
			ArtistaFactory artistaFactory= new ArtistaFactory();
			while (rs.next()) {
				artistas.add(artistaFactory.crearArtista(rs));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (ps != null) {
				ps.close();
			}
		}
		return artistas;
	}

	@Override
	public List<Artista> consultarUno() {
		return null;
	}

	public boolean existeArtista(String nombre) throws Exception {
		PreparedStatement ps = null;
		try {
			ConexionBD conexionBD = ConexionBD.getInstance();
			Connection con = conexionBD.getCon();
			ps = con.prepareStatement("select * from artista_art where cart_nombre=?");
			ps.setString(1, nombre);
			ResultSet rs=ps.executeQuery();
			return rs.next();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ps != null) {
				ps.close();
			}
		}
		return false;
	}

	@Override
	public void insertar(Artista dto) throws Exception {
		PreparedStatement ps = null;
		try {
			ConexionBD conexionBD = ConexionBD.getInstance();
			Connection con = conexionBD.getCon();
			boolean valor=	existeArtista(dto.getNombre());
			if(valor){
				return;
			}
			ps = con.prepareStatement("insert into artista_art(cart_id,cart_nombre) values(?,?)");
			String gen = GeneradorLlave.generarLLave();
			ps.setString(1, gen.substring(0, 32));
			ps.setString(2, dto.getNombre());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (ps != null) {
				ps.close();
			}
		}

	}

	@Override
	public Artista actualizar(Artista dto) {
		return null;
	}

	public byte[] createChecksum(String filename) throws Exception {
		InputStream fis = new FileInputStream(filename);

		byte[] buffer = new byte[1024];
		MessageDigest complete = MessageDigest.getInstance("MD5");
		int numRead;

		do {
			numRead = fis.read(buffer);
			if (numRead > 0) {
				complete.update(buffer, 0, numRead);
			}
		} while (numRead != -1);

		fis.close();
		return complete.digest();
	}

	// see this How-to for a faster way to convert
	// a byte array to a HEX string
	public String getMD5Checksum(String filename) throws Exception {
		byte[] b = createChecksum(filename);
		String result = "";

		for (int i = 0; i < b.length; i++) {
			result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
		}
		return result;
	}

}
